﻿using FreeRedis;
using Masuit.Tools;
using Newtonsoft.Json.Linq;
using Simple.Contracts.Contracts;
using Simple.Contracts.Enums.Common;
using Simple.Contracts.Models;
using Simple.Repository.SimpleEntitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Services.Services
{
    public class RedisService : IRedisService
    {
        private readonly RedisClient _redisClient;
        public RedisService(RedisClient redisClient)
        {
            _redisClient = redisClient;
        }

        public string GetAccountLoginTokenKey(long companyId, long employeeId, string token)
        {
            return $"com{companyId}:emp{employeeId}:token:{token}";
        }
        public void SetAccountLoginToken(string token, string value, long companyId, long employeeId, int timeoutSeconds)
        {
            var key = GetAccountLoginTokenKey(companyId, employeeId, token);
            _redisClient.Set<string>(key, value, timeoutSeconds);
        }
        public string GetAccountLoginToken(long companyId, long employeeId, string token)
        {
            var key = GetAccountLoginTokenKey(companyId, employeeId, token);
            return _redisClient.Get<string>(key);
        }

        /// <summary>
        /// 获取微信小程序全局唯一后台接口调用凭据-KEY
        /// </summary>
        /// <param name="weChatAppClient"></param>
        /// <returns></returns>
        public string GetWechatAccessTokenKey(WeChatAppClientEnum weChatAppClient)
        {
            return $"com0:wechat:{weChatAppClient.ToString().ToLower()}:accesstoken";
        }
        /// <summary>
        /// 设置微信小程序全局唯一后台接口调用凭据
        /// </summary>
        /// <param name="weChatAppClient"></param>
        public void SetWechatAccessToken(WeChatAppClientEnum weChatAppClient, string accesstoken, int timeoutSeconds)
        {
            var key = GetWechatAccessTokenKey(weChatAppClient);
            _redisClient.Set<string>(key, accesstoken, timeoutSeconds);
        }
        /// <summary>
        /// 获取微信小程序全局唯一后台接口调用凭据
        /// </summary>
        /// <param name="weChatAppClient"></param>
        /// <returns></returns>
        public string GetWechatAccessToken(WeChatAppClientEnum weChatAppClient)
        {
            var key = GetWechatAccessTokenKey(weChatAppClient);
            return _redisClient.Get<string>(key);
        }


        /// <summary>
        /// 获取登录验证码Key
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public string GetLoginCodeKey(string phone, string code)
        {
            return $"com0:login-code:{phone}:{code}";
        }
        /// <summary>
        /// 设置登录验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        public void SetLoginCodeKey(string phone, string code)
        {
            var key = GetLoginCodeKey(phone, code);
            _redisClient.Set<string>(key, code, 60 * 10);
        }
        /// <summary>
        /// 验证登录验证码是否有效
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool CheckLoginCodeIsEffective(string phone, string code)
        {
            var key = GetLoginCodeKey(phone, code);
            var value = _redisClient.Get<string>(key);
            return value == code;
        }
    }
}
